import * as types from '../action-types';
import { take, put, takeEvery, call, cps } from '../../redux-saga/effects';

const delay = ms => new Promise((resolve, reject) => {
    setTimeout(resolve, ms);
})

const ajax = (url) => {
    return fetch(url).then(res => res.json());
}

const delay2 = (ms, callback) => {
    setTimeout(() => {
        callback(null, 'xx');
    }, ms);
}

// worker saga
function *worker_add() {
    // yield delay(1000);
    // yield ajax('/time');
    // yield call(delay, 1000);
    yield cps(delay2, 1000);
    // put派发一个动作：store.dispatch({type: types.ADD})
    yield put({ type: types.ADD }); // { type: 'PUT', action: { type: 'ADD' } }
}

// watcher saga
function * watcher_add() {
    // 等待每一次的ASYNC_ADD动作派发，等到之后执行add这个saga
    // 单独开启一个子进程，不影响后面逻辑执行
    yield takeEvery(types.ASYNC_ADD, worker_add);
}

export default watcher_add;